package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;
import sun.security.x509.DNSName;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public List<Cart> listCart(Object... item) throws Exception {
        List<Cart> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer("select c.*,b.*,f.*" +
                " from cart c,business b,food f" +
                " where c.businessId = b.businessId and c.foodId = f.foodId" +
                " and c.userId = ?");
        if (item.length == 2){
            sql.append(" and c.businessId = ?");
        }
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            for (int i = 1; i <= item.length; i++) {
                pst.setObject(i, item[i - 1]);
            }
            rs = pst.executeQuery();
            while (rs.next()){
                Cart cart = new Cart();
                cart.setCartId(rs.getInt("cartId"));
                cart.setFoodId(rs.getInt("foodId"));
                cart.setBusinessId(rs.getInt("businessId"));
                cart.setUserId(rs.getString("userId"));
                cart.setQuantity(rs.getInt("quantity"));
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                cart.setBusiness(business);
                Food food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));
                cart.setFoods(food);
                list.add(cart);
            }
        } finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }



    @Override
    public int saveCart(Cart cart) throws Exception{
        int result = 0;
        String sql = "insert into cart(userId,businessId,foodId,quantity) value(?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            int count = 1;
            pst.setString(count++, cart.getUserId());
            pst.setInt(count++, cart.getBusinessId());
            pst.setInt(count++, cart.getFoodId());
            pst.setInt(count++, cart.getQuantity());
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public int updateCart(Cart cart) throws Exception {
        int result = 0;
        String sql = "update cart set quantity = ? where userId = ? and businessId = ? and foodId = ?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            int count = 1;
            pst.setInt(count++, cart.getQuantity());
            pst.setString(count++, cart.getUserId());
            pst.setInt(count++, cart.getBusinessId());
            pst.setInt(count++, cart.getFoodId());
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }

    //根据用户编号、商家编号、食品编号删除购物车表中的一条食品记录
    //根据用户编号、商家编号删除购物车表中的多条条记录
    @Override
    public int removeCart(Object... items) throws Exception {
        int result = 0;
        StringBuffer sql = new StringBuffer("delete from cart where userId = ? and businessId = ?");
        if (items.length == 3){
            sql.append(" and foodId = ?");
        }
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            for (int i = 1; i <= items.length; i++) {
                pst.setObject(i,items[i - 1]);
            }
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(rs,pst);
        }
        return result;
    }
}
